package userHelp

import (
	"common/helper"
	myredis "common/redis"
	g "github.com/doug-martin/goqu/v9"
)

var g_tbl_member_base string = "tbl_member_base:"

type TblMemberBase struct {
	Uid         string `db:"uid" json:"uid"`
	Username    string `db:"username" json:"username"`         // 用户名
	Password    string `db:"password" json:"password"`         // 密码
	CreatedAt   uint32 `db:"created_at" json:"created_at"`     // 创建时间
	CreatedIp   string `db:"created_ip" json:"created_ip"`     // 创建ip
	Phone       string `db:"phone" json:"phone"`               // 手机号
	Email       string `db:"email" json:"email"`               // 邮箱
	Tester      int    `db:"tester" json:"tester"`             // 1 正式账号 2 模拟账号 3代理
	Avatar      uint8  `db:"avatar" json:"avatar"`             // 头像id
	PayPassword string `db:"pay_password" json:"pay_password"` // 支付密码
	State       int    `db:"state" json:"state"`               // 1正常 2登陆锁定 3封号15分钟
	Remake      string `db:"remake" json:"remake"`             // 备注
	Prefix      string `db:"prefix" json:"prefix"`             // 前缀
	Pid         string `db:"pid" json:"pid"`                   // 会员唯一id(场馆使用)
	OperatorId  string `db:"operator_id" json:"operator_id"`   // 渠道id
	BusinessId  string `db:"business_id" json:"business_id"`   // 业务员id
	InviteCode  string `db:"invite_code" json:"invite_code"`
	LastLoginIp string `db:"last_login_ip" json:"last_login_ip"` // 上一次登陆ip
	LastLoginAt uint32 `db:"last_login_at" json:"last_login_at"` // 上一次登陆时间
	PswErrCnt   int    `db:"PswErrCnt" json:"PswErrCnt"`         // 密码输入错误次数
	BlockTime   uint32 `db:"BlockTime" json:"BlockTime"`         // 封号时间
	RealName    string `db:"real_name" json:"real_name"`         //真实名称
	Currency    string `db:"currency" json:"currency"`           //货币
}

func GetMemberRealName(uid string) string {
	LoadUserToRedis(uid)
	RealName := myredis.GetUserFieldString(uid, g_tbl_member_base+"real_name")
	return RealName
}

func GetCreatedIp(uid string) string {
	LoadUserToRedis(uid)
	return myredis.GetUserFieldString(uid, g_tbl_member_base+"created_ip")
}

func GetLastLoginIp(uid string) string {
	LoadUserToRedis(uid)
	return myredis.GetUserFieldString(uid, g_tbl_member_base+"last_login_ip")
}

func SetLastLoginIp(uid string, ip string) {
	LoadUserToRedis(uid)
	myredis.SetUserFieldValue(uid, g_tbl_member_base+"last_login_ip", ip)
}

func SetMemberOperatorId(uid string, optId string) {
	LoadUserToRedis(uid)
	myredis.SetUserFieldValue(uid, g_tbl_member_base+"operator_id", optId)
}

func SetMemberBusinessId(uid string, bussId string) {
	LoadUserToRedis(uid)
	myredis.SetUserFieldValue(uid, g_tbl_member_base+"business_id", bussId)
}

func GetMemberRemake(uid string) string {
	LoadUserToRedis(uid)
	remake := myredis.GetUserFieldString(uid, g_tbl_member_base+"remake")
	return remake
}

func GetMemberTester(uid string) int {
	LoadUserToRedis(uid)
	tester := myredis.GetUserFieldInt(uid, g_tbl_member_base+"tester")
	return tester
}

func GetMemberOperatorId(uid string) string {
	LoadUserToRedis(uid)
	OperatorId := myredis.GetUserFieldString(uid, g_tbl_member_base+"operator_id")
	if OperatorId == "" {
		OperatorId = "0"
	}
	return OperatorId
}

func GetMemberBusinessId(uid string) string {
	LoadUserToRedis(uid)
	BusinessId := myredis.GetUserFieldString(uid, g_tbl_member_base+"business_id")
	if BusinessId == "" {
		BusinessId = "0"
	}
	return BusinessId
}

func GetMemberState(uid string) int {
	LoadUserToRedis(uid)
	State := myredis.GetUserFieldInt(uid, g_tbl_member_base+"state")
	return State
}

func SetMemberState(uid string, state string) {
	LoadUserToRedis(uid)
	myredis.SetUserFieldValue(uid, g_tbl_member_base+"state", state)
}

func GetMemberPswErrCnt(uid string) int {
	LoadUserToRedis(uid)
	PswErrTimes := myredis.GetUserFieldInt(uid, g_tbl_member_base+"PswErrCnt")
	return PswErrTimes
}

func AddMemberPswErrCnt(uid string, AddPswErrCnt int64) {
	LoadUserToRedis(uid)
	myredis.AddUserFieldValueByInt64(uid, g_tbl_member_base+"PswErrCnt", AddPswErrCnt)
}

func SetMemberPswErrCnt(uid string, PswErrCnt string) {
	LoadUserToRedis(uid)
	myredis.SetUserFieldValue(uid, g_tbl_member_base+"PswErrCnt", PswErrCnt)
}

func GetMemberBlockTime(uid string) int32 {
	LoadUserToRedis(uid)
	BlockTime := myredis.GetUserFieldInt32(uid, g_tbl_member_base+"BlockTime")
	return BlockTime
}

func SetMemberBlockTime(uid string, BlockTime int64) {
	LoadUserToRedis(uid)
	myredis.SetUserFieldValueByInt64(uid, g_tbl_member_base+"BlockTime", BlockTime)
}

func GetMemberLoginPsw(uid string) string {
	LoadUserToRedis(uid)
	Password := myredis.GetUserFieldString(uid, g_tbl_member_base+"password")
	return Password
}

func SetMemberLoginPsw(uid string, loginPsw string) {
	LoadUserToRedis(uid)
	myredis.SetUserFieldValue(uid, g_tbl_member_base+"password", loginPsw)
}

func GetMemberName(uid string) string {
	LoadUserToRedis(uid)
	Username := myredis.GetUserFieldString(uid, g_tbl_member_base+"username")
	return Username
}

func IsTestAccount(AccountName string) bool {
	t := g.Dialect("mysql").From("tbl_member_test")
	ex := g.ExOr{}
	ex["account_name"] = AccountName
	query, _, _ := t.Select("uid").Where(ex).ToSQL()
	var uid string
	err := myredis.GetSlaveDBInstance().Get(&uid, query)

	if err == nil {
		helper.InfoLog("IsTestAccount true")
		return true
	}

	helper.InfoLog("IsTestAccount false")
	return false
}

func GetMemberIdByAccountName(AccountName string, prefix string) (string, bool) {
	t := g.Dialect("mysql").From("tbl_member_base")
	ex := g.ExOr{}
	if AccountName != "" {
		ex["username"] = AccountName
	}

	if prefix != "" {
		ex["prefix"] = prefix
	}

	query, _, _ := t.Select("uid").Where(ex).ToSQL()
	var uid string
	err := myredis.GetSlaveDBInstance().Get(&uid, query)

	if err == nil {
		return uid, true
	}

	return "0", false
}

func GetMemberPid(uid string) string {
	LoadUserToRedis(uid)
	Pid := myredis.GetUserFieldString(uid, g_tbl_member_base+"Pid")
	return Pid
}

func SetMemberPid(uid string, Pid string) {
	LoadUserToRedis(uid)
	myredis.SetUserFieldValue(uid, g_tbl_member_base+"Pid", Pid)
}

func GetMemberPayPsw(uid string) string {
	LoadUserToRedis(uid)
	Password := myredis.GetUserFieldString(uid, g_tbl_member_base+"pay_password")
	return Password
}

func SetMemberPayPsw(uid string, payPsw string) {
	LoadUserToRedis(uid)
	myredis.SetUserFieldValue(uid, g_tbl_member_base+"pay_password", payPsw)
}

func GetMemberPhone(uid string) string {
	LoadUserToRedis(uid)
	Phone := myredis.GetUserFieldString(uid, g_tbl_member_base+"phone")
	return Phone
}

func GetMemberEmail(uid string) string {
	LoadUserToRedis(uid)
	Email := myredis.GetUserFieldString(uid, g_tbl_member_base+"email")
	return Email
}

func SetMemberEmail(uid string, Email string) {
	LoadUserToRedis(uid)
	myredis.SetUserFieldValue(uid, g_tbl_member_base+"email", Email)
}

func SetMemberPhone(uid string, phone string) {
	LoadUserToRedis(uid)
	myredis.SetUserFieldValue(uid, g_tbl_member_base+"phone", phone)
}

func SetMemberName(uid string, username string) {
	LoadUserToRedis(uid)
	myredis.SetUserFieldValue(uid, g_tbl_member_base+"username", username)
}

func GetMemberCreateAt(uid string) uint32 {
	LoadUserToRedis(uid)
	createdAt := myredis.GetUserFieldUint32(uid, g_tbl_member_base+"created_at")
	return createdAt
}

func GetMemberLastLogin(uid string) uint32 {
	LoadUserToRedis(uid)
	lastLoginAt := myredis.GetUserFieldUint32(uid, g_tbl_member_base+"last_login_at")
	return lastLoginAt
}

/*
此函数尽量少调用 粒度太大
*/
func GetMemberBaseInfoByUid(uid string) (TblMemberBase, bool) {

	tblMemBaseInfo := TblMemberBase{}
	LoadUserToRedis(uid)
	if myredis.IsUserExis(string(uid)) {
		tblMemBaseInfo.Uid = uid
		tblMemBaseInfo.Pid = myredis.GetUserFieldString(uid, g_tbl_member_base+"Pid")
		tblMemBaseInfo.Username = myredis.GetUserFieldString(uid, g_tbl_member_base+"username")
		tblMemBaseInfo.Password = myredis.GetUserFieldString(uid, g_tbl_member_base+"password")
		tblMemBaseInfo.CreatedAt = myredis.GetUserFieldUint32(uid, g_tbl_member_base+"created_at")
		tblMemBaseInfo.CreatedIp = myredis.GetUserFieldString(uid, g_tbl_member_base+"created_ip")
		tblMemBaseInfo.Phone = myredis.GetUserFieldString(uid, g_tbl_member_base+"phone")
		tblMemBaseInfo.Email = myredis.GetUserFieldString(uid, g_tbl_member_base+"email")
		tblMemBaseInfo.Tester = myredis.GetUserFieldInt(uid, g_tbl_member_base+"tester")
		tblMemBaseInfo.Avatar = myredis.GetUserFieldUint8(uid, g_tbl_member_base+"invite_code")
		tblMemBaseInfo.PayPassword = myredis.GetUserFieldString(uid, g_tbl_member_base+"pay_password")
		tblMemBaseInfo.State = myredis.GetUserFieldInt(uid, g_tbl_member_base+"state")
		tblMemBaseInfo.Remake = myredis.GetUserFieldString(uid, g_tbl_member_base+"remake")
		tblMemBaseInfo.Prefix = myredis.GetUserFieldString(uid, g_tbl_member_base+"prefix")
		tblMemBaseInfo.OperatorId = myredis.GetUserFieldString(uid, g_tbl_member_base+"operator_id")
		tblMemBaseInfo.BusinessId = myredis.GetUserFieldString(uid, g_tbl_member_base+"business_id")
		tblMemBaseInfo.LastLoginIp = myredis.GetUserFieldString(uid, g_tbl_member_base+"last_login_ip")
		tblMemBaseInfo.LastLoginAt = myredis.GetUserFieldUint32(uid, g_tbl_member_base+"last_login_at")
		tblMemBaseInfo.RealName = myredis.GetUserFieldString(uid, g_tbl_member_base+"real_name")
		return tblMemBaseInfo, true
	} else {
		return tblMemBaseInfo, false
	}
}

func GetMemberIdByAccountNameAndPassword(AccountName, password, prefix string) (string, bool) {

	helper.InfoLog("GetMemberIdByAccountNameAndPassword:%s-%s-%s", AccountName, password, prefix)
	t := g.Dialect("mysql").From("tbl_member_base")
	ex := g.Ex{}
	if AccountName != "" {
		ex["username"] = AccountName
	}

	if password != "" {
		ex["password"] = password
	}

	if prefix != "" {
		ex["prefix"] = prefix
	}

	query, _, _ := t.Select("uid").Where(ex).ToSQL()
	var uid string
	err := myredis.GetSlaveDBInstance().Get(&uid, query)

	if err == nil {
		return uid, true
	} else {
		helper.InfoLog("GetMemberIdByAccountNameAndPassword error:%s---%s", err.Error(), query)
	}

	return "0", false
}
